#ifndef UART_H
#define UART_H

/*
** constants and macros
*/

/** @brief  UART Baudrate Expression
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600
 */
#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)

/** @brief  UART Baudrate Expression for ATmega double speed mode
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600
 */
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)

/**
 * Circular TX&RX Buffer Size Setters or Multipliers
 *
 * 8 x Multiplier Selection. Default is 4 (4x8=32)
 *
 * Comment UART_BUFFER_MULTIPLER and define UART_BUFFER_SET for a solid
 * set buffer size. Keep the buffer size a power of 2
 */
#define UART_BUFFER_MULTIPLIER 8
//#define UART_BUFFER_SET 32

#ifdef UART_BUFFER_MULTIPLIER
	#ifndef UART_RX_BUFFER_SIZE
		#define UART_RX_BUFFER_SIZE (8*UART_BUFFER_MULTIPLIER)
	#endif
	#ifndef UART_TX_BUFFER_SIZE
		#define UART_TX_BUFFER_SIZE (8*UART_BUFFER_MULTIPLIER)
	#endif
#else
	#ifdef UART_BUFFER_SET
		#ifndef UART_RX_BUFFER_SIZE
			#define UART_RX_BUFFER_SIZE UART_BUFFER_SET
		#endif
		#ifndef UART_TX_BUFFER_SIZE
			#define UART_TX_BUFFER_SIZE UART_BUFFER_SET
		#endif
	#else
		#ifndef UART_RX_BUFFER_SIZE
			#define UART_RX_BUFFER_SIZE 32
		#endif
		#ifndef UART_TX_BUFFER_SIZE
			#define UART_TX_BUFFER_SIZE 32
		#endif
	#endif
#endif

/* test if the size of the circular buffers fits into SRAM */
#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
#endif

/*
** high byte error return code of uart_getc()
*/
#define UART_FRAME_ERROR      0x0800              /* Framing Error by UART       */
#define UART_OVERRUN_ERROR    0x0400              /* Overrun condition by UART   */
#define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */
#define UART_NO_DATA          0x0100              /* no receive data available   */

/**
   @brief   Initialize UART and set baudrate
   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
   @return  none
*/
extern void uart_init(unsigned int baudrate);


/**
 *  @brief   Get received byte from ringbuffer
 *
 * Returns in the lower byte the received character and in the
 * higher byte the last receive error.
 * UART_NO_DATA is returned when no data is available.
 *
 *  @param   void
 *  @return  lower byte:  received byte from ringbuffer
 *  @return  higher byte: last receive status
 *           - \b 0 successfully received data from UART
 *           - \b UART_NO_DATA
 *             <br>no receive data available
 *           - \b UART_BUFFER_OVERFLOW
 *             <br>Receive ringbuffer overflow.
 *             We are not reading the receive buffer fast enough,
 *             one or more received character have been dropped
 *           - \b UART_OVERRUN_ERROR
 *             <br>Overrun condition by UART.
 *             A character already present in the UART UDR register was
 *             not read by the interrupt handler before the next character arrived,
 *             one or more received characters have been dropped.
 *           - \b UART_FRAME_ERROR
 *             <br>Framing Error by UART
 */
extern unsigned int uart_getc(void);


/**
 *  @brief   Put byte to ringbuffer for transmitting via UART
 *  @param   data byte to be transmitted
 *  @return  none
 */
extern void uart_putc(unsigned char data);


/**
 *  @brief   Put string to ringbuffer for transmitting via UART
 *
 *  The string is buffered by the uart library in a circular buffer
 *  and one character at a time is transmitted to the UART using interrupts.
 *  Blocks if it can not write the whole string into the circular buffer.
 *
 *  @param   s string to be transmitted
 *  @return  none
 */
extern void uart_puts(const char *s );


/**
 * @brief    Put string from program memory to ringbuffer for transmitting via UART.
 *
 * The string is buffered by the uart library in a circular buffer
 * and one character at a time is transmitted to the UART using interrupts.
 * Blocks if it can not write the whole string into the circular buffer.
 *
 * @param    s program memory string to be transmitted
 * @return   none
 * @see      uart_puts_P
 */
extern void uart_puts_p(const char *s );

/**
 * @brief    Macro to automatically put a string constant into program memory
 */
#define uart_puts_P(__s)       uart_puts_p(PSTR(__s))


/**
 * @brief    uart_puthex_***
 * 							nibble
 * 							byte
 * 							short
 * 							long
 *
 * @param    4bit, 8bit, 16bit, 32bit value
 * @return   none - output hex value via UART
 * @see      none
 */
extern void uart_puthex_nibble( const unsigned char b );
extern void uart_puthex_byte( const unsigned char b );
extern void uart_puthex_short( const uint16_t lbyte );
extern void uart_puthex_long( const uint32_t l );

/**
 * @brief    uart_putbin_byte
 *
 * @param    uint8 value
 * @return   none - Outputs a 8bit value as binary via UART
 * @see      none
 */
extern void uart_putbin_byte(uint8_t _val);

/**
 * @brief    uart_putint_***
 * 							byte
 * 							short
 * 							long
 * 							long_f
 *
 * @param    8bit, 16bit, 32bit intiger values
 * @return   none
 * @see      none
 */
extern void uart_putint_byte(uint8_t _val);
extern void uart_putint_short(uint16_t _val);
extern void uart_putint_long(uint32_t _val);
extern void uart_putint_long_f(uint32_t _val);

// Back Compat Support
#define f_uiWdec_uint32_f(x) uart_putint_long_f(x)
#define f_uiWdec_uint32(x) uart_putint_long(x)
#define f_uiWdec_uint16(x) uart_putint_short(x)
#define f_uiWdec_uint8(x) uart_putint_byte(x)


#endif // UART_H

